import MatterManager from "../../managers/MatterManager";
import GlobalEvent from "../../global/GlobalEvent";
import IPointer from "../../interfaces/IPointer";

export default class MatterBody extends Laya.Script {
    /** @prop {name:x, tips:"x坐标", type:Int, default:0}*/
    public x: number;
    /** @prop {name:x, tips:"y坐标", type:Int, default:0}*/
    public y: number;
    /** @prop {name:restitution, tips:"弹力系数", type:Int, default:0}*/
    public restitution: number;
    /** @prop {name:friction, tips:"摩擦力", type:Int, default:0}*/
    public friction: number;
    /** @prop {name:frictionStatic, tips:"静态摩擦力", type:Int, default:0}*/
    public frictionStatic: number;
    /** @prop {name:frictionAir, tips:"空气阻力", type:Int, default:0}*/
    public frictionAir: number;
    /** @prop {name:isStatic, tips:"是否是静止的", type:Bool, default:false}*/
    public isStatic: boolean = false;

    constructor() { super(); }

    protected matter = MatterManager.instance.matter;
    protected engine = MatterManager.instance.engine;

    public body: any;

    onEnable(): void {
        GlobalEvent.instance.on('matter_collision_start', this, (event) => {
            for (const pair of event.pairs) {
                if (pair.bodyA === this.body || pair.bodyB === this.body) {
                    this.onCollision(event);
                    break;
                }
            }
        });
    }

    onDisable(): void {
        if (this.body) {
            this.matter.World.remove(this.engine.world, this.body);
        }
    }

    onUpdate() {
        if (!this.body) {
            return;
        }
        let owner = this.owner as Laya.Sprite;
        owner.x = this.body.position.x;
        owner.y = this.body.position.y;
        owner.rotation = this.body.angle * 180 / Math.PI;
    }

    public setVelocity(x: number, y: number): void {
        if (!this.body) {
            return;
        }
        MatterManager.instance.setVelocity(this.body, x, y);
    }

    public setPosition(x: number, y: number): void {
        if (!this.body) {
            return;
        }
        MatterManager.instance.setPosition(this.body, x, y);
    }

    public setAngle(angle) {
        if (!this.body || angle === this.body.angle) {
            return;
        }

        MatterManager.instance.setAngle(this.body, angle);
    }

    public translate(x, y) {
        MatterManager.instance.translate(this.body, x, y);
    }

    public onCollision(event: any) {
        console.log('matter body  onCollision');
    }


    /**
     * 添加body到World
     */
    public addToWorld(): void {
        MatterManager.instance.addToWorld(this.body);
    }


    public getVectorByAngle(angle: number, speed: number) {
        let x = Math.cos(angle) * speed;
        let y = -Math.sin(angle) * speed;
        return {
            x, y
        }
    }

    public getCollisitionPair(paris) {
        let collisitionPair;

        for (const pair of paris) {
            if (pair.bodyA === this.body || pair.bodyB === this.body) {
                collisitionPair = pair.bodyA === this.body ? pair.bodyB : pair.bodyA;
                break;
            }
        }
        return collisitionPair;
    }

    public setScale(scale: number, point: any) {
        MatterManager.instance.setScale(this.body, scale, point);
    }

    public distanceOf2Point(point1: IPointer, point2: IPointer) {
        let xDistance = point1.x - point2.x;
        let yDistance = point1.y - point2.y;
        return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
    }
}